<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-alert type="success" title="无需输入实验编号,系统会自动为您生成实验编号" class="dialog-alert" />
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="实验编号" prop="id">
							<el-input v-model="state.ruleForm.id" placeholder="无需输入班级编号" clearable disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="实验名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入班级名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="教室" prop="classroom">
							<el-input v-model="state.ruleForm.classroom" placeholder="请输入教室" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="开始时间" prop="starttime">
							<el-date-picker
								v-model="state.ruleForm.starttime"
								format="YYYY/MM/DD HH:mm:ss"
								type="datetime"
								placeholder="请输入实验开始时间"
								clearable
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="结束时间" prop="endtime">
							<el-date-picker
								v-model="state.ruleForm.endtime"
								format="YYYY/MM/DD HH:mm:ss"
								type="datetime"
								placeholder="请输入实验结束时间"
								clearable
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" :offset="1" class="mb20">
						<el-form-item label="介绍" prop="description">
							<el-input :rows="5" :maxlength="100" type="textarea" v-model="state.ruleForm.description" placeholder="请输入介绍" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="experimentsManagementDialog">
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { updateExperimentInfo } from '/@/api/experiments/management';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const roleDialogFormRef = ref(null);
const state = reactive({
	ruleForm: {
		id: '',
		name: '',
		classroom: '',
		starttime: '',
		endtime: '',
		description: '',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 验证规则
const rules = reactive({
	name: [{ required: true, message: '实验名称不得为空!', trigger: 'blur' }],
	classroom: [{ required: true, message: '教室不得为空!', trigger: 'blur' }],
	starttime: [{ type: 'date', required: true, message: '开始时间不得为空!', trigger: 'change' }],
	endtime: [{ type: 'date', required: true, message: '结束时间不得为空!', trigger: 'change' }],
	description: [{ required: true, message: '介绍不得为空!', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (type: string, row: any) => {
	state.ruleForm = row;
	state.dialog.title = '修改实验';
	state.dialog.submitTxt = '修 改';
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	roleDialogFormRef.value.validate((isValid) => {
		if (isValid) {
			const updateInfo = {
				experiment_id: state.ruleForm.id,
				info: {
					experiment_name: state.ruleForm.name,
					classroom: state.ruleForm.classroom,
					starttime: state.ruleForm.starttime,
					endtime: state.ruleForm.endtime,
					description: state.ruleForm.description,
				},
			};
			updateExperimentInfo(updateInfo).then((res) => {
				if (res.code === '2000') {
					ElMessage.success('修改实验信息成功!');
					emit('refresh');
					closeDialog();
				} else {
					ElMessage.error('修改实验信息失败!');
				}
			});
		} else {
			ElMessage.error('请检查表单数据!');
		}
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}

	.dialog-alert {
		width: 700px;
		margin-left: 15px;
		margin-bottom: 20px;
	}
}
</style>
